﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class Section : ITemplate, IXmlBuildable, IChildDeletable
    {
        public Enumerations.SectionAlignment? Alignment { get; set; }
        public Enumerations.SectionType? Type { get; set; }
        public List<Row> Rows { get; set; }

        public Section()
        {
            this.Rows = new List<Row>();
        }

        public Section(XmlNode node):this()
        {
            try
            {
                this.Alignment = (Enumerations.SectionAlignment)Enum.Parse(typeof(Enumerations.SectionAlignment), XmlHelper.GetAttribute(node, "Alignment"));
                this.Type = (Enumerations.SectionType)Enum.Parse(typeof(Enumerations.SectionType), XmlHelper.GetAttribute(node, "Type"));

                foreach (XmlNode rowNode in node.SelectNodes("Row"))
                {
                    Row row = new Row(node.SelectSingleNode("Row"));
                    this.Rows.Add(row);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode layoutNode)
        {
            try
            {
                XmlNode sectionNode = XmlHelper.AddNode(layoutNode, "Section");
                XmlHelper.AddAttribute(sectionNode, "Alignment", this.Alignment);
                XmlHelper.AddAttribute(sectionNode, "Type", this.Type);

                foreach (Row row in this.Rows)
                {
                    row.AddNode(sectionNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode sectionNode = new TreeNode("Section");
                sectionNode.Tag = this;

                foreach (Row row in this.Rows)
                {
                    row.BuildTreeViewElement(sectionNode);
                }

                parentNode.Nodes.Add(sectionNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Row");

            if (this.Type.Value == Enumerations.SectionType.OneRow)
            {
                tsmi.Enabled = this.Rows.Count < 1;
            }
            else if (this.Type.Value == Enumerations.SectionType.TwoRow)
            {
                tsmi.Enabled = this.Rows.Count < 2;
            }
            else if (this.Type.Value == Enumerations.SectionType.ThreeRow)
            {
                tsmi.Enabled = this.Rows.Count < 3;
            }
            else
            {
                tsmi.Enabled = false;
            }

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;
            cms.Items.Add(tsmi);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                if (!this.Type.HasValue)
                {
                    MessageBox.Show("Please define a 'Section Type' for this section before adding rows", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    Row row = new Row();
                    row.BuildTreeViewElement(selectedNode);

                    this.Rows.Add(row);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Row)
                {
                    this.Rows.Remove((Row)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
